home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / vbcc / opt.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  24KB  |  576 lines

  1. /*  $VER: vbcc (opt.c) V0.4     */
  2. /*  allgemeine Routinen fuer den Optimizer und Steuerung der einzelnen  */
  3. /*  Laeufe                                                              */
  4.  
  5. #include "opt.h"
  6.  
  7. static char FILE_[]=__FILE__;
  8.  
  9. /*  die naechsten Funktionen sollten evtl. in ic.c                  */
  10.  
  11. /*  Sind use/change-Listen initialisiert?   */
  12. int have_alias;
  13.  
  14. void insert_IC(struct IC *p,struct IC *new)
  15. /*  fuegt new hinter p ein; p darf 0 sein                           */
  16. {
  17.     new->prev=p;
  18.     if(p){ new->next=p->next; p->next=new; }
  19.      else{ new->next=first_ic; first_ic=new; }
  20.     if(new->next) new->next->prev=new; else last_ic=new;
  21.     new->q1.am=new->q2.am=new->z.am=0;
  22. }
  23.  
  24. #ifndef NO_OPTIMIZER
  25.  
  26. int gchanged;   /*  Merker, ob Optimierungslauf etwas geaendert hat */
  27. int norek;      /*  diese Funktion wird nicht rekursiv auf          */
  28. int nocall;     /*  diese Funktion kehrt nicht zum Caller zurueck   */
  29.  
  30. /*  temporary fuer verschiedene Bitvektoren */
  31. unsigned char *tmp;
  32.  
  33. void recalc_offsets(struct flowgraph *fg)
  34. /*  berechnet Offsets fuer auto-Variablen neu und versucht, fuer Variablen, */
  35. /*  die nicht gleichzeitig aktiv sind, den gleichen Platz zu belegen        */
  36. {
  37.     int i,b,*eqto;size_t bsize;zlong *al,*sz;
  38.     unsigned char **used,*tmp,*empty;
  39.     struct IC *p;
  40.     if(DEBUG&1024) printf("recalculating offsets\n");
  41.     if(DEBUG&1024) printf("setting up arrays\n");
  42.     bsize=(basic_blocks+CHAR_BIT-1)/CHAR_BIT;
  43.     if(DEBUG&1024) printf("bsize=%lu\n",(unsigned long)bsize);
  44.     tmp=mymalloc(bsize);
  45.     al=mymalloc(sizeof(*al)*(vcount-rcount));
  46.     eqto=mymalloc(sizeof(int)*(vcount-rcount));
  47.     sz=mymalloc(sizeof(*sz)*(vcount-rcount));
  48.     empty=mymalloc(bsize);
  49.     memset(empty,0,bsize);
  50.     used=mymalloc(sizeof(unsigned char *)*(vcount-rcount));
  51.     /*  Tabelle, welche Variable in welchem Block belegt ist, aufbauen  */
  52.     for(i=0;i<vcount-rcount;i++){
  53.         if(zlleq(l2zl(0L),vilist[i]->offset)&&(vilist[i]->storage_class==AUTO||vilist[i]->storage_class==REGISTER)){
  54.             if(DEBUG&2048) printf("setting up for %s,%ld\n",vilist[i]->identifier,zl2l(vilist[i]->offset));
  55.             used[i]=mymalloc(bsize);
  56.             memset(used[i],0,bsize);
  57.         }else{
  58.             used[i]=0;
  59.         }
  60.         sz[i]=szof(vilist[i]->vtyp);
  61.         al[i]=falign(vilist[i]->vtyp);
  62.         eqto[i]=-1;
  63.     }
  64.     b=0;
  65.     while(fg){
  66.         if(b>=basic_blocks) ierror(0);
  67.         for(i=0;i<vcount-rcount;i++){
  68.             if(used[i]&&(BTST(fg->av_in,i)||BTST(fg->av_out,i))){
  69.                 int r;
  70.                 BSET(used[i],b);
  71.                 for(r=1;r<=MAXR;r++)
  72.                     if(fg->regv[r]&&fg->regv[r]->index==i) BCLR(used[i],b);
  73.             }
  74.         }
  75.         for(p=fg->start;p;p=p->next){
  76.             if((p->q1.flags&(VAR|REG))==VAR){
  77.                 i=p->q1.v->index;
  78.                 if(used[i]){
  79.                     BSET(used[i],b);
  80.                 }
  81.             }
  82.             if((p->q2.flags&(VAR|REG))==VAR){
  83.                 i=p->q2.v->index;
  84.                 if(used[i]){
  85.                     BSET(used[i],b);
  86.                 }
  87.             }
  88.             if((p->z.flags&(VAR|REG))==VAR){
  89.                 i=p->z.v->index;
  90.                 if(used[i]){
  91.                     BSET(used[i],b);
  92.                 }
  93.             }
  94.             if(p==fg->end) break;
  95.         }
  96.         fg=fg->normalout;
  97.         b++;
  98.     }
  99.     /*  schauen, ob Variablen in gleichen Speicher koennen  */
  100.     if(DEBUG&1024) printf("looking for distinct variables\n");
  101.     for(i=0;i<vcount-rcount;i++){
  102.         if(!used[i]||eqto[i]>=0) continue;
  103.         if(!memcmp(used[i],empty,bsize)){ free(used[i]);used[i]=0;continue;}
  104.         for(b=i+1;b<vcount-rcount;b++){
  105.             if(!used[b]||eqto[b]>=0) continue;
  106.             if(DEBUG&2048) printf("comparing %s(%ld) and %s(%ld)\n",vilist[i]->identifier,zl2l(vilist[i]->offset),vilist[b]->identifier,zl2l(vilist[b]->offset));
  107.  
  108.             memcpy(tmp,used[i],bsize);
  109.             bvintersect(tmp,used[b],bsize);
  110.             if(!memcmp(tmp,empty,bsize)){
  111.                 if(DEBUG&1024) printf("memory for %s(%ld) and %s(%ld) equal\n",vilist[i]->identifier,zl2l(vilist[i]->offset),vilist[b]->identifier,zl2l(vilist[b]->offset));
  112.                 eqto[b]=i;
  113.                 if(!zlleq(al[b],al[i])) al[i]=al[b];
  114.                 if(!zlleq(sz[b],sz[i])) sz[i]=sz[b];
  115.                 bvunite(used[i],used[b],bsize);
  116.             }
  117.         }
  118.     }
  119.     if(DEBUG&1024) printf("final recalculating\n");
  120.     max_offset=l2zl(0L);
  121.     for(i=0;i<vcount-rcount;i++){
  122.         if(!used[i]) continue;
  123.         free(used[i]);
  124.         if(DEBUG&2048) printf("adjusting offset for %s,%ld\n",vilist[i]->identifier,zl2l(vilist[i]->offset));
  125.         if(eqto[i]>=0){
  126.             vilist[i]->offset=vilist[eqto[i]]->offset;
  127.             continue;
  128.         }
  129.         vilist[i]->offset=zlmult(zldiv(zladd(max_offset,zlsub(al[i],l2zl(1L))),al[i]),al[i]);
  130.         max_offset=zladd(vilist[i]->offset,sz[i]);
  131.     }
  132.     free(used);
  133.     free(sz);
  134.     free(al);
  135.     free(tmp);
  136.     free(empty);
  137.     free(eqto);
  138. }
  139. void remove_IC_fg(struct flowgraph *g,struct IC *p)
  140. /*  Entfernt IC p und beachtet Flussgraph. Ausserdem werden             */
  141. /*  use/change-Listen freigegeben.                                      */
  142. {
  143.     if(p->q1.am||p->q2.am||p->z.am) ierror(0);
  144.     if(have_alias){
  145.         free(p->use_list);
  146.         free(p->change_list);
  147.     }
  148.     if(g->start==g->end){
  149.         g->start=g->end=0;
  150.     }else{
  151.         if(p==g->end) g->end=p->prev;
  152.         if(p==g->start) g->start=p->next;
  153.     }
  154.     remove_IC(p);
  155. }
  156.  
  157. int peephole(void)
  158. /*  macht alle moeglichen Vereinfachungen/Vereinheitlichungen   */
  159. {
  160.     struct IC *p;struct obj o;int t,c,null,eins,changed,done=0;
  161.     do{
  162.         if(DEBUG&1024) printf("searching for peephole optimizations\n");
  163.         changed=0;ic_count=0;
  164.         p=first_ic;
  165.         while(p){
  166.             c=p->code;
  167.             t=p->typf;
  168.             ic_count++;
  169.         if(c==LABEL&&report_suspicious_loops&&p->next&&p->next->code==BRA&&p->next->typf==t){
  170.           error(208);report_suspicious_loops=0;
  171.         }
  172.             if(p->q1.flags&KONST){
  173.                 if((p->q2.flags&KONST)||!p->q2.flags){
  174.                     struct IC *old=p->prev;
  175.                     if(fold(p)){ changed=1; p=old;continue;}
  176.                     p=p->next;continue;
  177.                 }else{
  178.                     if(c==ADD||c==MULT||(c>=OR&&c<=AND)){ /*  const nach rechts   */
  179.                         if(DEBUG&1024){ printf("swapped commutative op:\n");pric2(stdout,p);}
  180.                         o=p->q1;p->q1=p->q2;p->q2=o;
  181.                     }
  182.                 }
  183.             }
  184.             if(p->q2.flags&KONST){
  185.             /*  algebraische Optimierungen  */
  186.                 eval_const(&p->q2.val,t);
  187.                 if(zleqto(vlong,l2zl(0L))&&zuleqto(vulong,ul2zul(0UL))&&zdeqto(vdouble,d2zd(0.0))) null=1; else null=0;
  188.                 if(zleqto(vlong,l2zl(1L))&&zuleqto(vulong,ul2zul(1UL))&&zdeqto(vdouble,d2zd(1.0))) eins=1; else eins=0;
  189.                 if(zleqto(vlong,l2zl(-1L))&&zdeqto(vdouble,d2zd(-1.0))) eins=-1;
  190.                 if(eins<0&&(c==MULT||c==DIV)){
  191.                     if(DEBUG&1024){ printf("MULT/DIV with (-1) converted to MINUS:\n");pric2(stdout,p);}
  192.                     p->code=c=MINUS;p->q2.flags=0;
  193.                     changed=1;
  194.                 }
  195. #if 0
  196.                 if(c==SUB){
  197.                 /*  VORSICHT: Das funktioniert bei bestimmten Werten nicht! */
  198.                     if(DEBUG&1024){ printf("SUB converted to ADD:\n");pric2(stdout,p);}
  199.                     p->code=c=ADD; calc(MINUS,t,&p->q2.val,0,&p->q2.val,p);
  200.                     changed=1;
  201.                 }
  202. #endif
  203.                 if((eins>0&&(c==MULT||c==DIV))||(null&&(c==ADD||c==SUB||c==ADDI2P||c==SUBIFP||c==LSHIFT||c==RSHIFT||c==OR||c==XOR))){
  204.                     if(DEBUG&1024){ printf("operation converted to simple assignment:\n");pric2(stdout,p);}
  205.                     if(c==ADDI2P||c==SUBIFP) p->typf=t=POINTER;
  206.                     p->code=c=ASSIGN;p->q2.flags=0;p->q2.val.vlong=sizetab[t&NQ];
  207.                     changed=1;
  208.                 }
  209.                 if(null&&(c==MULT||c==DIV||c==MOD||c==AND)){
  210.                     if(c==DIV||c==MOD){ err_ic=p;error(210);err_ic=0;}
  211.                     if(DEBUG&1024){ printf("operation converted to ASSIGN 0:\n");pric2(stdout,p);}
  212.                     o.val.vlong=l2zl(0L);eval_const(&o.val,LONG);
  213.                     insert_const2(&p->q1.val,t);p->q1.flags=KONST;
  214.                     p->code=c=ASSIGN;p->q2.flags=0;p->q2.val.vlong=sizetab[t&NQ];
  215.                     changed=1;
  216.                 }
  217.                 if(((t&NQ)<=LONG||fp_assoc)&&(c==ADD||c==ADDI2P||c==MULT||c==LSHIFT||c==RSHIFT||c==OR||c==AND)){
  218.                 /*  assoziative Operatoren  */
  219.                     struct IC *n=p->next;
  220.                     if(n&&n->code==c&&(n->q2.flags&KONST)&&n->typf==t&&n->q1.flags==p->z.flags&&n->q1.v==p->z.v&&zleqto(n->q1.val.vlong,p->z.val.vlong)){
  221.                         if(DEBUG&1024){ printf("using associativity with:\n");pric2(stdout,p);pric2(stdout,p->next);}
  222.                         n->q1=p->q1;
  223.                         if(c==LSHIFT||c==RSHIFT||c==ADDI2P)
  224.                             calc(ADD,t,&p->q2.val,&n->q2.val,&n->q2.val,0);
  225.                         else
  226.                             calc(c,t,&p->q2.val,&n->q2.val,&n->q2.val,0);
  227.                         changed=1;
  228.                         if(p->q1.flags==p->z.flags&&p->q1.v==p->z.v&&zleqto(p->q1.val.vlong,p->z.val.vlong)){
  229.                             if(DEBUG&1024) printf("must remove first operation\n");
  230.                             n=p;p=p->next;
  231.                             if(have_alias){ free(n->use_list); free(n->change_list); }
  232.                             remove_IC(n);continue;
  233.                         }
  234.                     }
  235.                 }
  236.                 if((c==ADDI2P||c==SUBIFP)&&(p->q1.flags&VARADR)){
  237.                 /*  add #var,#const -> move #var+const      */
  238.                     union atyps val;
  239.                     if(DEBUG&1024){printf("add/sub #var,#const changed to assign:\n");pric2(stdout,p);}
  240.                     eval_const(&p->q2.val,t);
  241.                     insert_const2(&val,LONG);
  242.                     if(c==ADDI2P) calc(ADD,LONG,&p->q1.val,&val,&p->q1.val,0);
  243.                         else      calc(SUB,LONG,&p->q1.val,&val,&p->q1.val,0);
  244.                     p->code=c=ASSIGN;
  245.                     p->q2.flags=0;
  246.                     p->typf=t=POINTER;
  247.                     p->q2.val.vlong=sizetab[t&NQ];
  248.                     changed=1;
  249.                 }
  250.                 if((c==ADD||c==SUB)&&(t&NQ)<=LONG&&p->next&&p->next->next){
  251.                     struct IC *p1=p->next,*p2=p1->next;
  252.                     if(p1->code==MULT&&p2->code==ADDI2P&&
  253.                        p1->typf==t&&p2->typf==t&&
  254.                        (p1->q2.flags&KONST)&&(p->z.flags&(SCRATCH|DREFOBJ))==SCRATCH&&(p1->z.flags&(SCRATCH|DREFOBJ))==SCRATCH&&
  255.                        !compare_objs(&p->z,&p1->q1,t)&&
  256.                        !compare_objs(&p1->z,&p2->q2,t)){
  257.                         if(DEBUG&1024){ printf("rearranging array-access:\n");pric2(stdout,p);pric2(stdout,p1);pric2(stdout,p2);}
  258.                         p1->q1=p->q1;
  259.                         p->q1=p2->q1;
  260.                         p2->q1=p2->z;
  261.                         p->z=p2->z;
  262.                         calc(MULT,t,&p->q2.val,&p1->q2.val,&p->q2.val,0);
  263.                         if(c==ADD) p->code=ADDI2P; else p->code=SUBIFP;
  264.                         changed=1;continue;
  265.                     }
  266.                 }
  267.             }
  268.             if(p->q1.flags&KONST){
  269.             /*  algebraische Optimierungen  */
  270.                 eval_const(&p->q1.val,t);
  271.                 if(zleqto(vlong,l2zl(0L))&&zuleqto(vulong,ul2zul(0UL))&&zdeqto(vdouble,d2zd(0.0))) null=1; else null=0;
  272.                 if(null&&(c==DIV||c==MOD||c==LSHIFT||c==RSHIFT)){
  273.                     if(DEBUG&1024){ printf("operation converted to ASSIGN 0:\n");pric2(stdout,p);}
  274.                     o.val.vlong=l2zl(0L);eval_const(&o.val,LONG);
  275.                     insert_const2(&p->q1.val,t);p->q1.flags=KONST;
  276.                     p->code=c=ASSIGN;p->q2.flags=0;p->q2.val.vlong=sizetab[t&NQ];
  277.                     changed=1;
  278.                 }
  279.             }
  280.             if(!USEQ2ASZ&&p->z.flags&&!compare_objs(&p->q2,&p->z,p->typf)){
  281.                 if(c==ADD||c==MULT||(c>=OR&&c<=AND)){
  282.                     struct obj o;
  283.                     if(DEBUG&1024){printf("swapping objs because USEQ2ASZ\n");pric2(stdout,p);}
  284.                     o=p->q2;p->q2=p->q1;p->q1=o;
  285.                     /*  kein changed hier!  */
  286.                 }else{pric2(stdout,p); ierror(0);}
  287.             }
  288.             if((c==ADD||c==SUB)&&p->next){
  289.                 struct IC *p1=p->next;
  290.                 if(p1->code==ADDI2P&&p1->typf==t&&(p->z.flags&(SCRATCH|DREFOBJ))==SCRATCH&&!compare_objs(&p->z,&p1->q2,t)){
  291.                     if(DEBUG&1024){ printf("rearranging array-access:\n");pric2(stdout,p);pric2(stdout,p1);}
  292.                     p1->q2=p->q1;
  293.                     p->q1=p1->q1;
  294.                     p->z=p1->z;
  295.                     p1->q1=p1->z;
  296.                     if(c==ADD) p->code=c=ADDI2P; else p->code=c=SUBIFP;
  297.                     changed=1;continue;
  298.                 }
  299.             }
  300.             if((c==SUB||c==DIV||c==MOD)&&!compare_objs(&p->q1,&p->q2,p->typf)){
  301.                 /*  x-x=0, x/x=1, x%x=0 */
  302.                 if(DEBUG&1024){ printf("i-i, i/i, i%%i converted to ASSIGN 0/1:\n");pric2(stdout,p);}
  303.                 if(c==DIV) o.val.vlong=l2zl(1L); else o.val.vlong=l2zl(0L);
  304.                 eval_const(&o.val,LONG);insert_const2(&p->q1.val,t);p->q1.flags=KONST;
  305.                 p->code=c=ASSIGN;p->q2.flags=0;p->q2.val.vlong=sizetab[t&NQ];
  306.                 changed=1;
  307.             }
  308.             if(c==ASSIGN&&(p->z.flags&VAR)&&p->z.flags==p->q1.flags&&p->z.v==p->q1.v&&zleqto(p->z.val.vlong,p->q1.val.vlong)){
  309.                 struct IC *d;
  310.                 if(DEBUG&1024){ printf("removing redundant move:\n");pric2(stdout,p);}
  311.                 changed=1;
  312.                 d=p; p=p->next;
  313.                 if(have_alias){ free(d->use_list); free(d->change_list);}
  314.                 remove_IC(d); continue;
  315.             }
  316.             p=p->next;
  317.         }
  318.         if(changed) done|=changed;
  319.         gchanged|=changed;
  320.     }while(changed);
  321.     return(done);
  322. }
  323.  
  324. void insert_ccs(void)
  325. /*  Fuegt Variablen fuer ccs ein.   */
  326. {
  327.     struct IC *p; struct Var *v; struct Typ *t;
  328.     if(DEBUG&1024) printf("insert_ccs()\n");
  329.     for(p=first_ic;p;p=p->next){
  330.         if(p->code==COMPARE||p->code==TEST){
  331.             p->z.flags=VAR;
  332.             p->z.val.vlong=l2zl(0L);
  333.             t=mymalloc(TYPS);
  334.             t->flags=0;
  335.             t->next=0;
  336.             v=add_tmp_var(t);
  337.             p->z.v=v;
  338.             p=p->next;
  339.             if(p->code<BEQ||p->code>BGT){
  340.                 p=p->prev;
  341.                 p->code=NOP;
  342.                 p->q1.flags=p->q2.flags=p->z.flags=0;
  343.             }else{
  344.                 p->q1.flags=VAR;
  345.                 p->q1.val.vlong=l2zl(0L);
  346.                 p->q1.v=v;
  347.             }
  348.         }
  349.     }
  350. }
  351.  
  352. #endif
  353. #define FREEAV free(av_globals);free(av_statics);free(av_drefs);free(av_address);
  354. void optimize(long flags,struct Var *function)
  355. /*  flags:   1=Register, 2=optimize, 4=cse/cp, 8=constant_propagation,  */
  356. /*          16=dead_assignments, 32=global-optimizations                */
  357. /*          64=blockweise Registervergabe, 128=loop_optimizations (nur  */
  358. /*             in Verbindung mit 32), 256=recalc_offsets                */
  359. {
  360. #ifndef NO_OPTIMIZER
  361.     struct flowgraph *fg=0;int r,pass=0;
  362.     if(!function) ierror(0);
  363.     norek=nocall=0;
  364.     report_suspicious_loops=report_weird_code=1;
  365.     if(!strcmp(function->identifier,"main")){norek=1;nocall=1;}
  366.     /*  falls main() rekursiv aufgerufen werden kann, muss nomain==0 sein   */
  367.  
  368. #else
  369.  
  370.     flags&=1;
  371.  
  372. #endif
  373.     if(flags&2){
  374. #ifndef NO_OPTIMIZER
  375.         /*  Variablen fuer ccs einsetzen.   */
  376.         if(multiple_ccs) insert_ccs();
  377.         /*  nur ein pass, wenn nur lokale Optimierungen */
  378.         if(!(flags&32)) maxoptpasses=1;
  379.         do{
  380.             gchanged=0;pass++;
  381.             av_globals=av_statics=av_address=av_drefs=0;
  382.             rd_globals=rd_statics=rd_address=rd_drefs=0;
  383.             ae_globals=ae_statics=ae_address=ae_drefs=0;
  384.             cp_globals=cp_statics=cp_address=cp_drefs=0;
  385.             dlist=0;vilist=0;elist=0;rd_parms=0;
  386.  
  387.             if(DEBUG&1024) printf("\noptimizer (function %s) pass %d\n",function->identifier,pass);
  388.             num_vars();
  389.             peephole();
  390.             fg=jump_optimization();
  391.             create_alias(fg);
  392.             if(DEBUG&2048) print_vi();
  393.             if(flags&8){
  394.                 do{
  395.                     num_defs();
  396.                     if(flags&32){
  397.                         rd_mode=0;
  398.                         reaching_definitions(fg);
  399.                         if(DEBUG&1024) print_flowgraph(fg);
  400.                     }
  401.                     r=constant_propagation(fg,flags&32);
  402.                     if(DEBUG&1024) {printf("constant_propagation returned %d\n",r);print_flowgraph(fg);}
  403.                     if(r){
  404.                         if(peephole()){free_flowgraph(fg);fg=jump_optimization();}
  405.                     }
  406.                 }while(r);
  407.             }
  408.             if(flags&4){
  409.                 int repeat;
  410.                 do{
  411.                     do{
  412.                         num_exp();
  413.                         if(DEBUG&1024) print_flowgraph(fg);
  414.                         repeat=r=cse(fg,0);    /*  local cse   */
  415.                         if(DEBUG&1024) printf("local cse returned %d\n",r);
  416.                         gchanged|=r;
  417.                         if(r){  /*  neue Variablen eingefuegt   */
  418.                             if(DEBUG&1024) printf("must repeat num_vars\n");
  419.                             free(vilist);
  420.                             FREEAV;
  421.                             num_vars();
  422.                         }
  423.                         do{
  424.                             num_copies();
  425.                             if(DEBUG&1024) print_flowgraph(fg);
  426.                             r=copy_propagation(fg,0);   /*  copy propagation    */
  427.                             if(DEBUG&1024) printf("local copy propagation returned %d\n",r);
  428.                             if(r&2){
  429.                                 if(DEBUG&1024) printf("must repeat num_vars\n");
  430.                                 free(vilist);
  431.                                 FREEAV;
  432.                                 num_vars();
  433.                             }
  434.                             gchanged|=r;repeat|=r;
  435.                         }while(r);
  436.                     }while(repeat);
  437.                     repeat=0;
  438.                     if(flags&32){
  439.                         num_exp();
  440.                         if(DEBUG&1024) print_flowgraph(fg);
  441.                         available_expressions(fg);
  442.                         if(DEBUG&1024) print_flowgraph(fg);
  443.                         r=cse(fg,1);gchanged|=r;repeat|=r;
  444.                         if(DEBUG&1024) printf("global cse returned %d\n",r);
  445.                         if(r){  /*  neue Variablen eingefuegt   */
  446.                             if(DEBUG&1024) printf("must repeat num_vars\n");
  447.                             free(vilist);
  448.                             FREEAV;
  449.                             num_vars();
  450.                             gchanged|=r;repeat|=r;
  451.                             do{
  452.                                 num_copies();
  453.                                 if(DEBUG&1024) print_flowgraph(fg);
  454.                                 r=copy_propagation(fg,0);   /*  copy propagation    */
  455.                                 if(DEBUG&1024) printf("local copy propagation returned %d\n",r);
  456.                                 if(r&2){
  457.                                     if(DEBUG&1024) printf("must repeat num_vars\n");
  458.                                     free(vilist);
  459.                                     FREEAV;
  460.                                     num_vars();
  461.                                 }
  462.                                 gchanged|=r;repeat|=r;
  463.                             }while(r);
  464.                         }
  465.                         num_copies();
  466.                         available_copies(fg);
  467.                         if(DEBUG&1024) print_flowgraph(fg);
  468.                         r=copy_propagation(fg,1);   /*  copy propagation    */
  469.                         if(DEBUG&1024) printf("global copy propagation returned %d\n",r);
  470.                         if(r&2){
  471.                             if(DEBUG&1024) printf("must repeat num_vars\n");
  472.                             free(vilist);
  473.                             FREEAV;
  474.                             num_vars();
  475.                         }
  476.                         gchanged|=r;repeat|=r;
  477.                     }
  478.                 }while(0/*repeat*/);
  479.             }
  480.             if((flags&160)==160){
  481.                 r=loop_optimizations(fg);
  482.                 gchanged|=r;
  483.                 fg=jump_optimization();
  484.             }
  485.             if((flags&16)||((flags&1)&&pass>=maxoptpasses)){
  486. /*                num_vars();*/
  487.                 free_alias(fg);
  488.                 create_alias(fg);
  489.                 active_vars(fg);
  490.                 if(DEBUG&1024) print_flowgraph(fg);
  491.                 if((flags&16)&&pass<=maxoptpasses){
  492.                     r=dead_assignments(fg);
  493.                     if(DEBUG&1024) printf("dead_assignments returned %d\n",r);
  494.                     gchanged|=r;
  495.                 }
  496.             }
  497.  
  498.  
  499.             if((!gchanged||pass>=maxoptpasses)){
  500.             /*  Funktion evtl. fuer inlining vorbereiten und    */
  501.             /*  Registervergabe                                 */
  502.                 int varargs=0,c;
  503.                 if((c=function->vtyp->exact->count)!=0&&(*function->vtyp->exact->sl)[c-1].styp->flags!=VOID)
  504.                     varargs=1;
  505.  
  506.                 /*  default-Wert fuer inline-Entscheidung   */
  507.                 if(!varargs&&(flags&4096)&&(only_inline||ic_count<=inline_size)){
  508.                 /*  fuer function inlinig vorbereiten   */
  509.                     struct IC *p,*new;
  510.                     if(DEBUG&1024) printf("function <%s> prepared for inlining(ic_count=%d)\n",function->identifier,ic_count);
  511.                     function->fi=new_fi();
  512.                     function->fi->first_ic=first_ic;
  513.                     function->fi->last_ic=last_ic;
  514.                     first_ic=last_ic=0;
  515.                     p=function->fi->first_ic;
  516.                     while(p){
  517.                         new=mymalloc(ICS);
  518.                         memcpy(new,p,ICS);
  519.                         if((p->code>=BEQ&&p->code<=BRA)||p->code==LABEL)
  520.                             new->typf-=lastlabel;
  521.                         add_IC(new);
  522.                         p=p->next;
  523.                     }
  524.                     p=first_ic;first_ic=function->fi->first_ic;function->fi->first_ic=p;
  525.                     p=last_ic;last_ic=function->fi->last_ic;function->fi->last_ic=p;
  526.                     function->fi->vars=0;
  527.                 }
  528.                 if(flags&1){
  529.                     local_regs(fg);
  530.                     if(DEBUG&1024) print_flowgraph(fg);
  531.                     loops(fg,1);
  532.                     if(DEBUG&1024) print_flowgraph(fg);
  533.                     fg=create_loop_headers(fg,1);
  534.                     if(DEBUG&1024) print_flowgraph(fg);
  535.                     fg=create_loop_footers(fg,1);
  536.                     if(DEBUG&1024) print_flowgraph(fg);
  537.                     loop_regs(fg);
  538.                     if(DEBUG&1024) print_flowgraph(fg);
  539. #if 0
  540.                     if(flags&64){
  541.                         block_regs(fg);
  542.                         if(DEBUG&1024) print_flowgraph(fg);
  543.                     }
  544. #endif
  545.                     insert_regs(fg);
  546.                 }
  547.                 if(flags&256) recalc_offsets(fg);
  548.             }
  549.  
  550.             free_alias(fg);
  551.             free_flowgraph(fg);
  552.             free(vilist);
  553.             FREEAV;
  554.  
  555.             if((flags&32)&&gchanged&&pass>=maxoptpasses) error(172,maxoptpasses);
  556.  
  557.         }while(gchanged&&pass<maxoptpasses);
  558.  
  559.         /*  nur, um nochmal ueberfluessige Labels zu entfernen  */
  560.         fg=construct_flowgraph();
  561.         free_flowgraph(fg);
  562.  
  563.         /*  Register bei Funktionsaufrufen sichern  */
  564.         insert_saves();
  565.  
  566. #endif
  567.  
  568.     }else{
  569.         /*  keine Optimierungen     */
  570.         if(flags&1) simple_regs();
  571.         load_simple_reg_parms();
  572.     }
  573.     lastlabel=label;
  574. }
  575.  
  576.